'use strict'

const Service = require('egg').Service

class UserAccessService extends Service {

  async login(payload) {
    const { ctx, service } = this
    const user = await service.user.findByUsername(payload.username)
    if(!user){
      ctx.throw(422, ctx.__('user not found'))
    }

    let verifyPsw = ctx.helper.md5(payload.password) == user.userpwd
    if(!verifyPsw) {
      ctx.throw(422, ctx.__('Incorrect user password'))
    }
    // 生成Token令牌
    return { access_token: await service.actionToken.apply(user) }
  }

  async logout() {
  }

}

module.exports = UserAccessService
